﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.UI;
using APC.Common; //Database Handling - UI Language - Icon Gallery

namespace APC
{
    public partial class frmSetOrderPriority : Telerik.WinControls.UI.RadForm
    {

        private IconLibrary ImageList;

        public frmSetOrderPriority()
        {
            InitializeComponent();
            ImageList = new IconLibrary();
        }

        private void frmSetOrderPriority_Load(object sender, EventArgs e)
        {
            Order o = new Order(true);
            o.SetOrderStateAll(Order.OrderState.NotReady);

            if (o.PendingOrderList.Count > 0)
            {
                foreach (APC.Common.Order or in o.PendingOrderList)
                {
                    ListViewDataItem lst = new ListViewDataItem();
                    lst.Tag = or.Id;
                    lst.Image = SetIcon("order");
                    lst.SubItems.Add(""); //Icon
                    lst.SubItems.Add(or.OrderNumber);
                    lst.SubItems.Add(or.Name);
                    lstOrderPriority.Items.Add(lst);
                }
            }
            else
            {
                ListViewDataItem lst = new ListViewDataItem();            
                lst.SubItems.Add(""); //Icon
                lst.SubItems.Add("No pending orders...!");
                lstOrderPriority.Items.Add(lst);
            }
        }

        /// <summary>
        /// Returns icon based on icon name. E.g."folder"
        /// </summary>
        /// <param name="MenuIcon"></param>
        /// <returns></returns>
        private Image SetIcon(String MenuIcon = "")
        {
            int i;
            Image img = null;

            i = ImageList.Img16x16.Images.IndexOfKey(MenuIcon.Trim());

            if (i > -1)
            {
                img = ImageList.Img16x16.Images[i];
            }
            return img;
        }

        private void btnCancelOrderPriority_Click(object sender, EventArgs e)
        {
            Order o = new Order();
            o.SetOrderStateAll(Order.OrderState.ReadyForProduction);
            this.Close();
        }

        private void btnOrderSetPriority_Click(object sender, EventArgs e)
        {
            Order o = new Order();
            o.SetOrderStateAll(Order.OrderState.ReadyForProduction);

            this.Close();

        }

        private void frmSetOrderPriority_FormClosing(object sender, FormClosingEventArgs e)
        {
            Order o = new Order();
            o.SetOrderStateAll(Order.OrderState.ReadyForProduction);
        }

        private void btnOrderSequenceDown_Click(object sender, EventArgs e)
        {
            //ListViewDataItem selectedItem = lstOrderPriority.SelectedItem;
            
            //if (selectedItem != null)
            //{
            //   int index =  lstOrderPriority.Items.IndexOf(selectedItem);
            //   index += 1;

            //   if (index >= lstOrderPriority.Items.Count)
            //   {
            //       index = lstOrderPriority.Items.Count - 1; 
            //   }

            //   lstOrderPriority.Items.Remove(selectedItem);
            //   lstOrderPriority.Items.Insert(index, selectedItem);  
            //}
        }

        private void btnOrderSequenceUp_Click(object sender, EventArgs e)
        {
            //try
            //{
            //    if (lstOrderPriority.SelectedIndex > 0)
            //    {
            //        int index = lstOrderPriority.SelectedIndex;
            //        ListViewDataItem current = lstOrderPriority.SelectedItem as ListViewDataItem;
            //        lstOrderPriority.Items.Remove(current);
            //        lstOrderPriority.Items.Insert(index - 1, current);
            //        lstOrderPriority.SelectedIndex = index - 1;
            //    }
            //}
            //catch (Exception ex)
            //{
            //}
        }

    }
}
